using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
    class UserMessageAdminDA {

        private const string SP_CountBySentUserKey = "dbo.[UserMessageAdmin_CountBySentUserKey]";
        private const string SP_GetAllWithPaging = "dbo.[UserMessageAdmin_GetAllWithPaging]";
        private const string SP_GetBySentUserKeyWithPaging = "dbo.[UserMessageAdmin_GetBySentUserKeyWithPaging]";

        public static UserMessageAdminEnt CreateFromReader(IDataReader reader) {
            UserMessageAdminEnt item = new UserMessageAdminEnt();
            item.ID = ConvertUtils.ToLong(reader["ID"]);
            item.ConversationID = ConvertUtils.ToLong(reader["ConversationID"]);
            item.SentUserKey = ConvertUtils.ToString(reader["SentUserKey"]);
            item.ReceivedUserKey = ConvertUtils.ToString(reader["ReceivedUserKey"]);
            item.Title = ConvertUtils.ToString(reader["Title"]);
            item.Content = ConvertUtils.ToString(reader["Content"]);
            item.Status = (EUserMessageStatus)ConvertUtils.ToByte(reader["Status"]);
            item.Type = (EUserMessageType)ConvertUtils.ToByte(reader["Type"]);
            item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
            item.SenderDeleted = ConvertUtils.ToBool(reader["SenderDeleted"]);
            item.ReceiverDeleted = ConvertUtils.ToBool(reader["ReceiverDeleted"]);
            item.SenderName = ConvertUtils.ToString(reader["SenderName"]);
            item.SenderGender = (EUserGender)ConvertUtils.ToByte(reader["SenderGender"]);
            item.SenderAvatarFileName = ConvertUtils.ToString(reader["SenderAvatarFileName"]);
            item.SenderAvatarImageStoreID = ConvertUtils.ToInt(reader["SenderAvatarImageStoreID"]);
            item.SenderAvatarStatus = (EUserAvatarStatus)ConvertUtils.ToByte(reader["SenderAvatarStatus"]);
            item.ReceiverName = ConvertUtils.ToString(reader["ReceiverName"]);
            item.ReceiverGender = (EUserGender)ConvertUtils.ToByte(reader["ReceiverGender"]);
            item.ReceiverAvatarFileName = ConvertUtils.ToString(reader["ReceiverAvatarFileName"]);
            item.ReceiverAvatarImageStoreID = ConvertUtils.ToInt(reader["ReceiverAvatarImageStoreID"]);
            item.ReceiverAvatarStatus = (EUserAvatarStatus)ConvertUtils.ToByte(reader["ReceiverAvatarStatus"]);

            return item;
        }

        public static int CountBySentUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = 0;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_CountBySentUserKey)) {
                db.AddParameter(cmd, "SentUserKey", userKey);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    if (reader.Read()) {
                        result = ConvertUtils.ToInt(reader["Count"]);
                    }
                }
            }
            return result;
        }

        public static List<UserMessageAdminEnt> GetAllWithPaging(long startRow, long endRow, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserMessageAdminEnt> result = new List<UserMessageAdminEnt>();
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetAllWithPaging)) {
                db.AddParameter(cmd, "StartRow", startRow);
                db.AddParameter(cmd, "EndRow", endRow);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        result.Add(CreateFromReader(reader));
                    }
                }
            }
            return result;
        }

        public static List<UserMessageAdminEnt> GetBySentUserKeyWithPaging(string userKey, long startRow, long endRow, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            List<UserMessageAdminEnt> result = new List<UserMessageAdminEnt>();
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_GetBySentUserKeyWithPaging)) {
                db.AddParameter(cmd, "SentUserKey", userKey);
                db.AddParameter(cmd, "StartRow", startRow);
                db.AddParameter(cmd, "EndRow", endRow);
                using (IDataReader reader = db.ExecuteReader(cmd)) {
                    while (reader.Read()) {
                        result.Add(CreateFromReader(reader));
                    }
                }
            }
            return result;
        }
    }
}